/**
 * gwt-validator 
 *
 * Copyright (C) 2006  Nikolaus Rumm, Christian Simonutti
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package gwt.validator.client.action;

import gwt.validator.client.exception.ValidationIssue;

import com.google.gwt.user.client.ui.Label;

/**
 * Action that adds the issue's message in the given text widget.
 * 
 * @author <a href="mailto:nikolaus.rumm@gmail.com">Nikolaus Rumm</a>
 */
public class AddMessageAction extends BaseAction {

	/**
	 * The default prefix
	 */
	public final static String PREFIX = "";

	/**
	 * The default postfix
	 */
	public final static String POSTFIX = "";

	/**
	 * The default line separator
	 */
	public final static String LINE_SEPARATOR = "\n";

	/**
	 * The label that will display the message
	 */
	private Label label;

	/**
	 * The prefix that will be prepended to the added message
	 */
	private String prefix;

	/**
	 * The postfix that will be appended to the added message
	 */
	private String postfix;

	/**
	 * The line separator that will be prepended to the added message if the
	 * label's existing text is not empty
	 */
	private String lineSeparator;

	/**
	 * Constructor
	 * 
	 * @param aLabel
	 *            The label that will display the message
	 */
	public AddMessageAction(Label aLabel) {
		this.label = aLabel;
		this.lineSeparator = "\n";
	}

	/**
	 * @see Action#onSuccess()
	 */
	public void onSuccess() {
		// noop
	}

	/**
	 * @see Action#onFailure(ValidationIssue)
	 */
	public void onFailure(ValidationIssue aIssue) {
		StringBuffer sb = new StringBuffer(this.label.getText());
		if (sb.length() > 0) {
			sb.append(this.lineSeparator);
		}
		if (this.prefix != null) {
			sb.append(this.prefix);
		}
		sb.append(aIssue.getMessage());
		if (this.postfix != null) {
			sb.append(this.postfix);
		}
		this.label.setText(sb.toString());
	}
}
